<template>
  <div class="vip-container">
    <!-- VIP 等级展示 -->
    <div class="vip-level">
      <transition name="fade-zoom">
        <span :key="curVip" class="vip-number">VIP{{ curVip }}</span>
      </transition>
      <div
        class="vip-upgrade-effect"
        v-if="showUpgradeEffect"
      ></div>
    </div>

    <!-- VIP 进度条 -->
    <div class="vip-progress-bar">
      <div class="vip-progress-inner" :style="{ width: realProgress + '%' }"></div>
    </div>

    <!-- 文案展示 -->
    <div class="vip-text">成长值：{{ realProgress }}%</div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted } from 'vue'

const curVip = ref(1)
const targetVip = ref(1)
const realProgress = ref(0)
const targetProgress = ref(0)
const showUpgradeEffect = ref(false)

/** 进度平滑递增动画 */
function animateProgress() {
  let timer = setInterval(() => {
    if (realProgress.value >= targetProgress.value) {
      clearInterval(timer)
      realProgress.value = targetProgress.value
    } else {
      realProgress.value += 1
    }
  }, 10)
}

/** 模拟数据变更 */
function simulateUpgrade(newVip: number, newProgress: number) {
  if (newVip > curVip.value) {
    showUpgradeEffect.value = true
    setTimeout(() => {
      showUpgradeEffect.value = false
    }, 1000)
  }
  curVip.value = newVip
  targetProgress.value = newProgress
  animateProgress()
}

onMounted(() => {
  // 初始化数据
  targetProgress.value = 30
  animateProgress()

  // 模拟 3 秒后升级
  setTimeout(() => {
    simulateUpgrade(2, 80)
  }, 3000)
})
</script>

<style scoped>
.vip-container {
  width: 300px;
  margin: 20px auto;
  padding: 16px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  position: relative;
}

.vip-level {
  position: relative;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 12px;
}

.vip-number {
  position: relative;
  z-index: 2;
}

.vip-upgrade-effect {
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, #00f0ff, transparent);
  border-radius: 50%;
  animation: shine 1s ease-out;
  z-index: 1;
}

@keyframes shine {
  0% {
    opacity: 1;
    transform: scale(0.5) translateX(-50%);
  }
  100% {
    opacity: 0;
    transform: scale(2) translateX(-50%);
  }
}

.vip-progress-bar {
  height: 20px;
  background: #eee;
  border-radius: 16px;
  overflow: hidden;
  margin-bottom: 8px;
}

.vip-progress-inner {
  height: 100%;
  background: linear-gradient(to right, #1989fa, #00c2ff);
  border-radius: 16px;
  transition: width 1s ease;
}

.vip-text {
  font-size: 14px;
  color: #666;
}

/* 数字过渡动画 */
.fade-zoom-enter-active {
  transition: all 0.5s ease;
}
.fade-zoom-enter-from {
  opacity: 0;
  transform: scale(0.5);
}
.fade-zoom-enter-to {
  opacity: 1;
  transform: scale(1);
}
</style>
